package com.tpfinal.agenda.core.mysqlrequests;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import com.tpfinal.agenda.controller.MainController;
import com.tpfinal.agenda.core.AgendaActionsIDs;
import com.tpfinal.agenda.model.City;
import com.tpfinal.agenda.model.factories.CitiesFactory;

public class GetAllCitiesRequest extends MySqlBaseRequest {

	private static final String QUERY = "SELECT * FROM localidades";

	@Override
	public int getID() {
		return AgendaActionsIDs.GET_ALL_CITIES;
	}

	@Override
	protected void setup(Map<String, Object> params) {
		this.sqlQuery = QUERY;
		this.queryType = QUERY_TYPE_SELECT;

	}

	@Override
	protected Map<String, Object> processResult(Map<String, Object> resultSet) {
		try {
			ResultSet mysqlResult = (ResultSet) resultSet
					.get(MySqlBaseRequest.MYSQL_RESULTSET);

			ArrayList<City> list = CitiesFactory
					.createCityListFromMySqlResult(mysqlResult);
			MainController.get().setCitiesList(list);

			connection.close();
			resultSet.put(RESULT_CODE, OK);
		} catch (SQLException e) {
			resultSet.put(RESULT_CODE, ERROR);
			resultSet.put(ERROR_MSG, e.getMessage());
		}
		return resultSet;
	}
}
